package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.Comment;
import com.burgeon.backend.SHOP.entity.Goods;
import com.burgeon.backend.SHOP.entity.Order;
import com.burgeon.backend.SHOP.mappers.GoodsMapper;
import com.burgeon.backend.SHOP.mappers.OrderMapper;
import com.burgeon.backend.SHOP.rest.CreateComment;
import com.burgeon.backend.SHOP.rest.FindComment;
import com.burgeon.backend.SHOP.service.AssessService;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.TemplateSwitch;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.TemplateMapper;
import com.burgeon.backend.service.QueueService;
import com.burgeon.backend.service.ex.ErrorDataException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AssessServiceimpl implements AssessService {
    private final static Log logger = LogFactory.getLog(AssessService.class);
    @Autowired
    private QueueService queueService;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private TemplateMapper templateMapper;

    @Override
    public List<Comment> GetGoodsComment(String gid,String token) {
        String code = goodsMapper.selectBygid(gid).getCode();
        Info info = GetInfo(token);
        FindComment findComment = new FindComment(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            return findComment.getData(code);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            System.err.println("获取商品评论错误");
            return null;
        }
    }

    @Override
    public void createComment(Comment comment ,String[] gid, String token) throws ErrorDataException {
        Info info = GetInfo(token);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String now = sdf.format(date);
        Order order = orderMapper.FindOrderERPData(comment.getOrderId());
        comment.setOrderId(order.getRetailCode());
        CreateComment createComment = new CreateComment(info.getUrl(), info.getAccount(), info.getPassword());

        for (String g : gid) {
            Goods goods = goodsMapper.getGoods(g);
            try {
                createComment.getData(comment, goods.getCode());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        String oid = order.getId();

        try {
            orderMapper.upOrderStateByID(oid, "4", now);//更改订单状态为已评论4
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            System.err.println("写入评论错误");
        }

        //todo 奖励积分 购物券
        TemplateSwitch templateSwitch = templateMapper.FindTemplateSwitch(token);

     /*   if ("Y".equals(templateSwitch.getSwitch9())){
            //加积分
            queueService.putNewUserIntegral(user.getName(), user.getPhone(), user.getOpenid(), templateSwitch.getValue9(), token);

        }

        if ("Y".equals(templateSwitch.getSwitch10())){
            //加购物券
            queueService.putNewUserCoupon(templateSwitch.getValue10(), user.getPhone(),token);

        }*/
    }

    private Info GetInfo(String token) {
        return accountMapper.GetTokenData(token);
    }
}